// src/router/index.js

import {createRouter, createWebHistory, createWebHashHistory} from 'vue-router'
import { defineAsyncComponent } from 'vue'

const router = createRouter({ 
  // history: createWebHashHistory(),  // hash 模式
  history: createWebHistory(),  // history 模式
  routes: [
    {
      path: '/',
      redirect: '/test1',
    },
    {
      path: '/test1',
      name: 'test1',
      component: defineAsyncComponent(() => import('../pages/test/index.vue')),
      meta: {
        title: '首页',
      },
    },
    {
      path: '/test2',
      name: 'home',
      component: defineAsyncComponent(() => import('../pages/test2/index.vue')),
      meta: {
        title: '首页',
      },
    }
  ]
})

// 全局路由守卫
router.beforeEach((to, from, next)=>{
  // console.log(to, from)
  if (to.meta.title) {
    document.title = '${to.meta.title}';
  }
  next()
})

router.afterEach((to, from)=>{
  // console.log(to, from)
  console.log('afterEach')
})

export default router